import { dateTimePicker as _dateTimePicker, getMonthDay } from '../../../utils/dateTimePicker.js';
const ins = getApp()

Page({
  data:{
    template: {
      role: ['我要兼职', '我要招聘'],
      role_text: ['SEEKER', 'EMPLOYER'],
      reward_unit: ['次', '小时', '半天', '天', '周', '月'],
      tag: ''
    },
    data_index: {
      role: 0,
      reward_unit: 0
    },
    form: {
      role: 'SEEKER',
      title: '',
      reward: '',
      reward_unit: '次',
      address: '',
      position: ins.globalData.position,
      content: '',
      tag: [],
      images: [],
      expire_date: '0000-00-00 00:00:00'
    },
    today: new Date()
  },
  textareaBInput(e) {
    this.setData({
      textareaBValue: e.detail.value
    })
  },
  chooseImage() {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        if(res.tempFiles.length > 0) {
          const obj = res.tempFiles[0]
          if(obj.size > 1024 * 1024 * 2) {
            wx.showToast({
              title: '单张图片不能超过2M',
              icon: 'none'
            })
          } else {
            ins.api.upload(obj.path, null, {
              success: resp => {
                if(resp.data.ret == ins.globalData.request_ret.SUCCESS) {
                  this.setData({
                    'form.images': this.data.form.images.concat(resp.data.data)
                  })
                } else {
                  wx.showToast({
                    title: resp.data.data == 'object' ? resp.data.data[0] : resp.data.msg,
                    icon: 'none'
                  })
                }
              },
              fail: resp => {
                wx.showToast({
                  title: '服务器错误',
                  icon: 'error'
                })
              }
            }, true)
          }
        }
      }
    });
  },
  viewImage(e) {
    wx.previewImage({
      urls: this.data.form.images,
      current: e.currentTarget.dataset.url
    });
  },
  deleteImage(e) {
    wx.showModal({
      title: '提示',
      content: '确定要删除这张图片吗？',
      cancelText: '取消',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          this.data.form.images.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            'form.images': this.data.form.images
          })
        }
      }
    })
  },
  valueChange(e) {
    if(e.currentTarget.dataset.field == 'role') {
      this.setData({
        ['data_index.role']: e.detail.value,
        ['form.role']: this.data.template.role_text[e.detail.value]
      })
    } else if(e.currentTarget.dataset.field == 'reward_unit') {
      this.setData({
        ['data_index.reward_unit']: e.detail.value,
        ['form.reward_unit']: this.data.template.reward_unit[e.detail.value]
      })
    } else if(e.currentTarget.dataset.field == 'date') {
        this.setData({
          'template.date': e.detail.value,
          'form.expire_date': e.detail.value + this.data.form.expire_date.substring(10)
        })
    } else if(e.currentTarget.dataset.field == 'time') {
      this.setData({
        'template.time': e.detail.value,
        'form.expire_date': this.data.form.expire_date.substring(0, 11) + e.detail.value
      })
    } else if(e.currentTarget.dataset.field == 'tag') {
      this.setData({
        'template.tag': e.detail.value,
        'form.tag': e.detail.value.split(' ').filter((v) => {return v.length > 0})
      })
    } else {
      this.setData({
        ['form.' + e.currentTarget.dataset.field]: e.detail.value
      })
    }
  },
  getLocation() {
    wx.chooseLocation({
      success: (e) => {
        this.setData({
          'form.address': e.address + " " + e.name,
          'form.position': {
            lat: e.latitude,
            lng: e.longitude
          }
        })
      },
    })
  },
  submit: function(){
    ins.api.requestApi('post/publish', 'POST', this.data.form, {
      success: resp => {
        if(resp.data.ret == ins.globalData.request_ret.SUCCESS) {
          wx.navigateBack({
            delta: 1,
            complete: () => {
              wx.showToast({
                title: '发布成功',
                icon: 'success'
              })
            }
          })
        } else {
          wx.showToast({
            title: resp.data.data == 'object' ? resp.data.data[0] : resp.data.msg,
            icon: 'none'
          })
        }
      },
      fail: resp => {
        wx.showToast({
          title: '服务器错误',
          icon: 'error'
        })
      }
    }, true)
  },
})